package org.edu.modules.center.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.center.entity.VeTeachHomework;
import org.edu.modules.center.service.IVeTeachHomeworkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"教师发布作业"})
@RestController
@RequestMapping({"/center/veTeachHomework"})
public class VeTeachHomeworkController
        extends BaseController<VeTeachHomework, IVeTeachHomeworkService>
{
    private static final Logger log = LoggerFactory.getLogger(VeTeachHomeworkController.class);
    @Autowired
    private IVeTeachHomeworkService veTeachHomeworkService;
    @Value("${edu.path.urlPre}")
    private String fileUrlPre;

    @AutoLog("教师发布作业-全部作业分页查询")
    @ApiOperation(value="全部作业分页查询", notes="教师发布作业-全部作业分页查询")
    @PostMapping({"/list"})
    public Result<?> queryPageList(VeTeachHomework veTeachHomework, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeTeachHomework> queryWrapper = QueryGenerator.initQueryWrapper(veTeachHomework, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veTeachHomework);
        IPage<Map<String, Object>> pageList = this.veTeachHomeworkService.pageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("教师发布作业-根据学生提交作业id查询考试成绩")
    @ApiOperation(value="根据作业id查询考试成绩", notes="教师发布作业-根据作业id查询考试成绩")
    @PostMapping({"/publicInfo/queryById"})
    @ApiOperationSupport(order=1)
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        Map<String, Object> mapVeTeachHomework = this.veTeachHomeworkService.getMapById(id);
        if (mapVeTeachHomework == null) {
            return Result.error("未找到对应数据");
        }
        String str = (String)mapVeTeachHomework.values().toArray()[2];
        String str1 = (String)mapVeTeachHomework.values().toArray()[6];
        String str2 = this.fileUrlPre + "pub/exam/download/" + str;
        String str3 = this.fileUrlPre + "pub/exam/download/" + str1;
        mapVeTeachHomework.put("teachsourceFile", str2);
        mapVeTeachHomework.put("studysourceFile", str3);
        return Result.OK(mapVeTeachHomework);
    }

    @AutoLog("教师发布作业-新增信息")
    @ApiOperation(value="新增信息", notes="教师发布作业-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeTeachHomework veTeachHomework)
    {
        this.veTeachHomeworkService.save(veTeachHomework);
        return Result.OK("添加成功！");
    }

    @AutoLog("教师发布作业-修改信息")
    @ApiOperation(value="修改信息", notes="教师发布作业-修改信息")
    @PostMapping({"/edit"})
    public Result<?> edit(@RequestBody VeTeachHomework veTeachHomework)
    {
        this.veTeachHomeworkService.updateById(veTeachHomework);
        return Result.OK("编辑成功!");
    }

    @AutoLog("教师发布作业-通过id删除")
    @ApiOperation(value="通过id删除", notes="教师发布作业-通过id删除")
    @PostMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veTeachHomeworkService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("教师发布作业-批量删除")
    @ApiOperation(value="批量删除", notes="教师发布作业-批量删除")
    @PostMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veTeachHomeworkService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @AutoLog("教师发布作业-待完成作业分页查询")
    @ApiOperation(value="待完成作业分页查询", notes="教师发布作业-待完成作业分页查询")
    @PostMapping({"/unfinishedList"})
    public Result<?> unfinishedList(VeTeachHomework veTeachHomework, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeTeachHomework> queryWrapper = QueryGenerator.initQueryWrapper(veTeachHomework, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veTeachHomework);
        IPage<Map<String, Object>> pageList = this.veTeachHomeworkService.unfinishPageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }
}
